package org.framework.session;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionContext {
	private static ThreadLocal<HttpSession> sessionHolder = new ThreadLocal<HttpSession>();

	private static HttpSession getSession() {		
		HttpServletRequest request = RequestContex.getRequest();
		if (request == null) {
			//for structs2
			return sessionHolder.get();
		}  
		return request.getSession(); //SpringMVC下直接获取
	}

	public static void setSession(HttpSession session) {
		sessionHolder.set(session);
	}

	public static String getUserId() {
		HttpSession session = getSession();
		if (session != null) {
			return (String) session.getAttribute("sessionUserId");
		} else {
			return null;
		}
	}

	public static String getUserId(HttpSession session) {
		if (session != null) {
			return (String) session.getAttribute("sessionUserId");
		} else {
			return null;
		}
	}

	public static String getOrgId() {
		HttpSession session = getSession();
		if (session != null) {
			return (String) session.getAttribute("sessionOrg");
		} else {
			return null;
		}
	}

	public static String getUserName() {
		HttpSession session = getSession();
		if (session != null) {
			return (String) session.getAttribute("sessionUserName");
		} else {
			return null;
		}
	}

	public static String getDeptId() {
		HttpSession session = getSession();
		if (session != null) {
			return (String) session.getAttribute("sessionDept");
		} else {
			return null;
		}
	}

	public static String getUserRole() {
		HttpSession session = getSession();
		if (session != null) {
			return (String) session.getAttribute("sessionRole");
		} else {
			return null;
		}
	}

	public static String getOrgLevel() {
		HttpSession session = getSession();
		if (session != null) {
			return (String) session.getAttribute("sessionOrgLevel");
		} else {
			return null;
		}
	}

	public static String getMenuId() {
		HttpSession session = getSession();
		if (session != null) {
			return (String) session.getAttribute("menuId");
		} else {
			return null;
		}
	}

	public static Object getSessionValue(String name) {
		HttpSession session = getSession();
		if (session != null) {
			return session.getAttribute("name");
		} else {
			return null;
		}
	}
}
